package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

//	Request URL: http://localhost:8500/api/album/trackInfo/uploadTrack
	//将本地的声音存放到高腾讯云vod中
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		//文件在vod中的id mediaFileId
		//文件在vod中的地址 mediaUrl
		Map<String,Object> map = trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}


	//Request URL: http://localhost:8500/api/album/trackInfo/saveTrackInfo
	@Operation(summary = "保存声音")
	@TingshuLogin
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}
	//Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPage/1/10
	@Operation(summary = "分页展示用户创作的声音列表")
	@PostMapping("/findUserTrackPage/{pn}/{pz}")
	@TingshuLogin
	public Result findUserTrackPage(@PathVariable(value = "pn") Long pn ,
									@PathVariable(value = "pz") Long pz,
									@RequestBody TrackInfoQuery trackInfoQuery){
		//1.构建分页对象（对返回给前端的数据进行分页）
		IPage<TrackListVo> page  = new Page<>(pn, pz);
		trackInfoQuery.setUserId(AuthContextHolder.getUserId());
		//2.将分页对象传进去（未来自动给Page对象的records属性赋值，list）
		trackInfoService.findUserTrackPage(page,trackInfoQuery);
		return Result.ok(page);
	}

//	Request URL: http://localhost:8500/api/album/trackInfo/getTrackInfo/51942

	@Operation(summary = "根据声音Id查询声音信息")
	@GetMapping("/getTrackInfo/{trackId}")
	@TingshuLogin
	public Result getTrackInfo(@PathVariable(value = "trackId")Long trackId){
		return Result.ok(trackInfoService.getById(trackId));
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/updateTrackInfo/51942
	@Operation(summary = "修改声音信息")
	@TingshuLogin
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateAlbumInfo(@PathVariable("trackId") Long trackId,@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateAlbumInfo(trackId,trackInfoVo);
		return Result.ok();
	}

	// Request URL:http://localhost:8500/api/album/trackInfo/findUserTrackPaidList/48241

	@Operation(summary = "分集展示要买的声音列表")
	@TingshuLogin
	@GetMapping("/findUserTrackPaidList/{currentTrackId}")
	public Result findUserTrackPaidList(@PathVariable(value = "currentTrackId")Long currentTrackId){
		List<Map<String ,Object>> list = trackInfoService.findUserTrackPaidList(currentTrackId);
		return Result.ok(list);

	}

	//	Request URL: http://localhost:8500/api/album/trackInfo/removeTrackInfo/51942
	@Operation(summary = "根据声音Id删除声音")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable("trackId") Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/findAlbumTrackPage/1429/1/10
	@GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
	@Operation(summary = "根据专辑Id查询该专辑下声音列表且显示付费图标")
	@TingshuLogin
	public Result findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId ,
									 @PathVariable(value = "pn") Long pn ,
									 @PathVariable(value = "pz") Long pz ){

		IPage<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pn,pz);
		albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage,albumId);
		return Result.ok(albumTrackListVoPage);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/getTrackStatVo/6447
	@GetMapping("/getTrackStatVo/{trackId}")
	public Result getTrackStatVo(@PathVariable(value = "trackId") Long trackId){

		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);

		return Result.ok(trackStatVo);
	}




}

